package com.nuonuo.es.controller;

import com.nuonuo.es.service.NewBuyerInfoService;
import com.nuonuo.es.service.OldBuyerInfoService;
import com.nuonuo.es.vo.BuyerBaseInfoDataVO;
import com.nuonuo.es.vo.BuyerBaseInfoVO;
import org.elasticsearch.action.search.SearchResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 优化前ES查询
 *
 * @author duan
 * @version 1.0
 * @date 2023/7/17 19:25
 */
@RestController
@RequestMapping(value = "/new/buyer/info")
public class NewBuyerInfoController {

    @Autowired
    private NewBuyerInfoService newBuyerInfoService;



    /**
     * 分页查询
     *
     * @param buyerName
     * @param saleTaxNum
     * @param pageStart
     * @param pageSize
     * @param totalHits
     * @return
     */
    @GetMapping(value = "/pageQuery")
    public BuyerBaseInfoDataVO queryTotalTwice(String buyerName, String saleTaxNum, int pageStart,
                                               int pageSize, boolean totalHits) {
        BuyerBaseInfoDataVO buyerBaseInfoData = new BuyerBaseInfoDataVO();
        int startRow = (pageStart -1) * pageSize;
        SearchResponse response = newBuyerInfoService
                .query(buyerName, saleTaxNum, startRow, pageSize, totalHits);
        buyerBaseInfoData.setTookMs(response.getTook().getMillis());
        buyerBaseInfoData.setTotal(response.getHits().getTotalHits());
        return buyerBaseInfoData;
    }


    /**
     * 判断某个税号下的客户是否存在
     * @param buyerName
     * @param saleTaxNum
     * @return
     */
    @GetMapping(value = "/findOne")
    public String findOne(String buyerName, String saleTaxNum) {
        boolean exist = newBuyerInfoService.exist(buyerName, saleTaxNum, null);
        return String.valueOf(exist);
    }

    /**
     * 判断某个税号下的客户是否存在
     * @param buyerName
     * @param saleTaxNum
     * @return
     */
    @GetMapping(value = "/findByBuyerNmAndSaleTaxnum")
    public String findByBuyerNmAndSaleTaxnum(String buyerName, String saleTaxNum) {
        SearchResponse response = newBuyerInfoService.findByBuyerNmAndSaleTaxnum(buyerName, saleTaxNum);
        if (response.getHits().getHits().length > 0) {
            return response.getHits().getHits()[0].getSourceAsString();
        }
        return null;
    }

    @GetMapping(value = "/fuzzyQueryByName")
    public  List<Map<String, Object>> fuzzyQueryByName(String buyerName, String saleTaxNum) {
        return newBuyerInfoService.fuzzyQueryByName(buyerName, saleTaxNum);
    }
}
